package com.xcdh.auth.service.impl;

import com.xcdh.auth.service.AuthService;
import com.xcdh.auth.util.JwtUtil;
import com.xcdh.auth.util.SecurityUtil;
import com.xcdh.client.UserClient;
import com.xcdh.commons.exception.XcdhAsser;
import com.xcdh.domain.vo.R;
import com.xcdh.domain.entity.UserDO;
import com.xcdh.domain.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private UserClient userClient ;

    @Override
    public R<UserVo> login(UserDO userDO) {
        R<UserDO> retValue = userClient.findUserByName(userDO.getUsername());
        UserDO user = retValue.getData();
        XcdhAsser.nullError(user , "用户名或密码错误");
        XcdhAsser.tureError(user.getLockFlag() != 0,"用户已锁定");
        XcdhAsser.falseError(SecurityUtil.verifyUser(user , userDO.getPassword()), "用户名或密码错误");
        UserVo userVo = new UserVo() ;
        BeanUtils.copyProperties(user , userVo);
        String token = JwtUtil.createToken(userVo);
        userVo.setToken(token);
        return R.okData(userVo);
    }
}
